﻿@model List<CommunityDTO>
@{
    ViewBag.Title = "小区管理";
}

<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 系统管理 <span class="c-gray en">&gt;</span> 小区管理 <a class="btn btn-success radius r btn-refresh" style="line-height:1.6em;margin-top:3px" href="javascript:;" onclick="javascript: location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
    <div class="text-c">
        <form id="formSeach" action="/Webadmin/Community/List" method="post">
            <span class="select-box" style="width:150px">
                @Html.DropDownList("SelectCity",(List<SelectListItem>)ViewBag.ListCitiy,"--- 所有城市 ---", new { id= "SelectCity", @class = "select",size="1" })
            </span>
            <span class="select-box" style="width:150px">
                <select class="select" id="SelectRegion" name="SelectRegion" size="1">
                    <option value="">所有小区</option>
                </select>
            </span>
            <input type="text" class="input-text" style="width:250px" placeholder="关键字" id="Keywords" name="Keywords">
            <button type="submit" class="btn btn-success" id="btnSeach"><i class="Hui-iconfont">&#xe665;</i> 查询</button>
        </form>
    </div>
    <div class="cl pd-5 bg-1 bk-gray mt-20"> <span class="l"><a href="javascript:;" onclick="batch_del()" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a> <a href="javascript:;" onclick=" layer_show('添加小区', '/Webadmin/Community/Add' , '800' , '500' )" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加小区</a></span> <span class="r">共有数据：<strong>@ViewBag.totalCount</strong> 条</span></div>
    <form id="formList">
        <table class="table table-border table-bordered table-hover table-bg">
            <thead>
                <tr>
                    <th scope="col" colspan="9">小区列表</th>
                </tr>
                <tr class="text-c">
                    <th width="25"><input type="checkbox" name="" value=""></th>
                    <th width="40">ID</th>
                    <th>城市</th>
                    <th>区域</th>
                    <th>小区</th>
                    <th>地址</th>
                    <th>建造年份</th>
                    <th width="100">操作</th>
                </tr>
            </thead>
            <tbody>
                @{
                    foreach (var community in Model)
                    {
                        <tr class="text-c">
                            <td><input type="checkbox" value="@community.Id" name="selectdIds"></td>
                            <td>@community.Id</td>
                            <td>@community.CityName</td>
                            <td>@community.RegionName</td>
                            <td>@community.Name</td>
                            <td>@community.Location</td>
                            <td>@community.BuiltYear</td>
                            <td class="td-manage">
                                <a title="编辑" href="javascript:;" onclick="layer_show('小区编辑', '/Webadmin/Community/Edit?id=@community.Id', '800', '500')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a>
                                <a title="删除" href="javascript:;" onclick="del(this, @community.Id)" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>
                            </td>
                        </tr>
                    }
                }
            </tbody>
        </table>
    </form>
    @{
        var pager = new ZSZ.Common.CommonPager();
        pager.CurrentPageClassName = "active";
        pager.MaxPagerCount = (int)ViewBag.maxPagerCount;
        pager.PageIndex = (int)ViewBag.pageIndex;//这些数据，cshtml不知道，就必须让Action传递给我们
                                                 //对于所有cshtml要用到，但是又获取不到的数据，都由Action来获取，然后放到ViewBag或者Model中传递给cshtml
        pager.PageSize = (int)ViewBag.pageSize;
        pager.TotalCount = (int)ViewBag.totalCount;
        pager.UrlPattern = "/WebAdmin/Community/List?pageIndex={pn}";
    }
    <div id="pager">
        @Html.Raw(pager.GetPagerHtml()) </ div >
    </div>
</div>

@section FooterScript
{
    <script type="text/javascript">
        $(function () {
            var $SelectCity = $("#SelectCity");
            //初始化小区数据
            loadRegions($SelectCity.val());
            //切换城市
            $SelectCity.change(function () {
                loadRegions($(this).val());
            });
        });
        //加载小区数据
        function loadRegions(cityId) {
            var $SelectRegion = $("#SelectRegion");
            $SelectRegion.empty();
            $SelectRegion.append("<option value=''>--- 所有小区 ---</option>");
            //cityId 为空
            if (cityId == "") {
                return false;
            }
            //cityId 不是数字
            if (isNaN(cityId)) {
                return false;
            }
            //非法 cityId
            if (parseInt(cityId)<=0) {
                return false;
            }
            $.post("/WebAdmin/Community/LoadRegions", { cityId:cityId },function(res){
                if (res.status == "ok") {
                    for (var index in res.data) {
                        $SelectRegion.append("<option value='" + res.data[index].id + "'>" + res.data[index].name + "</option>");
                    }
                }
            });
        }

        /*批量删除*/
        function batch_del() {
            layer.confirm("确认要批量删除吗？", function () {
                var formData = $("#formList").serializeArray();
                $.ajax({
                    url: "/WebAdmin/Community/BatchDelete",
                    type: "post",
                    data: formData,
                    dataType: "json",
                    success: function (res) {
                        if (res.status == "ok") {
                            layer.msg('已批量删除!', { icon: 1, time: 2000 }, function () {
                                $(".btn-refresh").click();
                            });
                        } else if (res.status == "redirect") {
                            location.href = res.data;
                        } else if (res.status == "error") {
                            layer.msg(res.errorMsg, { icon: 2, time: 1000 });
                        } else {
                            layer.msg("批量删除失败", { icon: 2, time: 1000 });
                        }
                    },
                    error: function () {
                        layer.msg('网络出错!', { icon: 2, time: 2000 });
                    }
                });
            });
        }

        /*删除*/
        function del(obj, id) {
            layer.confirm('确认要删除吗？', function (index) {
                $.ajax({
                    type: 'POST',
                    url: '/WebAdmin/Community/Delete',
                    data: { id: id },
                    dataType: 'json',
                    success: function (res) {
                        if (res.status == "ok") {
                            layer.msg('已删除!', { icon: 1, time: 2000 }, function () {
                                $(".btn-refresh").click();
                            });
                        } else if (res.status == "redirect") {
                            location.href = res.data;
                        } else if (res.status == "error") {
                            layer.msg(res.errorMsg, { icon: 2, time: 1000 });
                        } else {
                            layer.msg("删除失败", { icon: 2, time: 1000 });
                        }
                    },
                    error: function (data) {
                        layer.msg('网络出错!', { icon: 2, time: 2000 });
                    },
                });
            });
        }
    </script>
}

